﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.susi.util
{
    public static class ArrayExtensions
    {
        public static bool Remove<T>(this T[] ary,T item)where T : class
        {
            var len = ary.Length;
            for(int i = 0; i < len; i++)
            {
                if (ary[i] == item)
                {
                    ary[i] = null;
                    return true;
                }
            }
            return false;
        }

        public static void Clear<T>(this T[] ary)where T:class
        {
            var len = ary.Length;
            for (var i = 0; i < len; i++)
            {
                ary[i] = null;
            }            
        }
        /// <summary>
        /// 如果数组是零长数组则返回null，否则返回自己
        /// </summary>
        public static T[] TrimZeroLen<T>(this T[] ary)
        {
            if (ary == null) return null;
            if (ary.Length == 0) return null;            
            return ary;
        }

        /// <summary>
        /// 如果数组是元素全为null的数组则返回null，否则返回自己
        /// </summary>
        public static T[] TrimNullAry<T>(this T[] ary)where T:class
        {
            if (ary == null) return null;
            if (ary.Length == 0) return null;
            foreach(var item in ary)
            {
                if (item != null) return ary;
            }
            return null ;
        }
        
    }
}
